Skip to content

Add ability to parse egglog expressions into Python values #319

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Aug 7, 2025

Conversation

saulshanabrook
Copy link
Member

@saulshanabrook saulshanabrook commented Aug 7, 2025

This PR adds the ability to parse egglog expressions into Python values in a type safe manner.

See the documentation and tests for the new API.

This was done to try to make it easier to use egglog in situations like https://github.com/sdiehl/mlir-egglog where you want to extract a large expression, then traverse it in Python.

I went through many different iterations of this API, including making a few PRs to MyPy (python/mypy#19600 and python/mypy#19577) to try to find an API that was ergonomic and also as type safe as possible. Meaning that if you have a variable x of expression type Math and you want to see if is of function fn and get the args, then the return value of fn has to be Math and the args should be properly typed as well.

I would love if I could use Python builtin match statement but just use functions, but unfortunately it can only work with types not matching against other types of functions. At runtime, I could probably fiddle with things enough to make this pass, but statically it would still complain, which would defeat the purpose.

So instead, I settled on a function that you can call to return the args, which types them properly if it matches.

I also did add support for primitives inside match statements, and added support for custom classes to also support match statements with __match_args__. So if you have one "canonical" term for an expression, that it should get reduced to, you could match against that more easily.

To do this, I had to make egglog Expr subclasses be types, which they weren't before. To keep most of our setup the same, I added a metaclass that made them types, which gets a bit hairy and possibly should be removed or refactored at a later date.

I used @sdiehl's mlir-egglog project as an example to create this against. See the parsing in my fork of it to get a feel for how you could use this API.

@saulshanabrook saulshanabrook requested a review from Copilot August 7, 2025 13:11
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds a new type-safe mechanism for parsing egglog expressions into Python values. The key change replaces the deprecated .eval() method with a new .value property and introduces a comprehensive set of helper functions for deconstructing expressions.

Key changes:

  • Replace all .eval() calls with .value property access across tests and examples
  • Add new deconstruct.py module with type-safe expression parsing utilities
  • Implement pattern matching support for egglog expressions with __match_args__

Reviewed Changes

Copilot reviewed 16 out of 16 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
python/egglog/deconstruct.py New module providing functions to safely extract callable functions, arguments, variable names, and literal values from expressions
python/egglog/builtins.py Replace deprecated .eval() methods with .value properties and add pattern matching support via __match_args__
python/egglog/runtime.py Add metaclass infrastructure to support isinstance checks and pattern matching for runtime classes
python/tests/test_*.py Update all test files to use .value instead of .eval() for consistency
python/egglog/declarations.py Add match_args field to class declarations to support pattern matching
docs/reference/python-integration.md Update documentation to demonstrate new value extraction and pattern matching APIs

Copy link

codspeed-hq bot commented Aug 7, 2025

CodSpeed WallTime Performance Report

Merging #319 will not alter performance

Comparing add-deconstruct (8876276) with main (cbf9160)

Summary

✅ 7 untouched benchmarks

Copy link

codspeed-hq bot commented Aug 7, 2025

CodSpeed Instrumentation Performance Report

Merging #319 will not alter performance

Comparing add-deconstruct (8876276) with main (cbf9160)

Summary

✅ 7 untouched benchmarks

@saulshanabrook saulshanabrook merged commit 5b25a5e into main Aug 7, 2025
22 checks passed
@saulshanabrook saulshanabrook deleted the add-deconstruct branch August 7, 2025 14:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants